// pages/login/login.js
var util = require('../../utils/util.js');
import api from '../../api/api.js'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    pwd: "",
    loginInfo: {},
    loginType: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR;
        that.setData({
          winHeight: calc
        });
      }
    });
  },
  /**
   * 输入账号密码
   */
  loginInput: function (e) {
    if (e.target.dataset.name == "phone") {
      this.setData({
        phone: util.trim(e.detail.value)
      });
    }
    if (e.target.dataset.name == "pwd") {
      this.setData({
        pwd: util.trim(e.detail.value)
      });
    }
  },
  onLogin: function () {
    var self = this;
    if (util.trim(self.data.phone) == "" || !self.data.phone) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        confirmColor: "#e30c26",
        content: '请输入用户名'
      })
      return;
    }
    if (util.trim(self.data.pwd) == "" || !self.data.pwd) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        confirmColor: "#e30c26",
        content: '请输入密码'
      })
      return;
    }
    wx.showLoading({
      title: '正在登录...',
      mask: true
    })
    if (!self.data.loginType) {
      var jsonStr = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"phone":"' + self.data.phone + '","pwd":"' + self.data.pwd + '"}}';
      api.login({
        method: 'POST',
        data: {
          REQ_MESSAGE: jsonStr
        },
        complete:(res)=>{
          self.loginRes(res)
        },
        fail: () => {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            confirmColor: "#e30c26",
            showCancel: false,
            content: '登录失败'
          })
        }
      })
    } else {
      var json = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"vendor_phone":"' + self.data.phone + '","pass_word":"' + self.data.pwd + '"}}'
      //厂商登录
      api.businessLogin({
        method: 'POST',
        data: {
          REQ_MESSAGE: json
        },
        complete:(res)=>{
          self.loginRes(res)
        },
        fail: () => {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            confirmColor: "#e30c26",
            showCancel: false,
            content: '登录失败'
          })
        }
      })
    }
  },
  loginRes: function (res) {
    console.log(res)
    var that = this
    wx.hideLoading();
    if (res.statusCode == 200) {
      var data = res.data;
      if (data.REP_BODY.RSPCOD == "000000") {
        //储存登录信息
        data.REP_BODY.LOGINTYPE = that.data.loginType; //登录方式
        wx.setStorage({
          key: 'loginInfo',
          data: data,
          success: function () {
            wx.navigateBack();
          }
        })
      } else if (res.data.REP_BODY.RSPCOD) {
        wx.showModal({
          title: '提示',
          confirmColor: "#e30c26",
          showCancel: false,
          content: res.data.REP_BODY.RSPMSG||"登录失败"
        })
      } else {
        wx.showModal({
          title: '提示',
          confirmColor: "#e30c26",
          showCancel: false,
          content: '登录失败'
        })
      }
    } else if (res.statusCode == 404) {
      wx.showModal({
        title: '提示',
        confirmColor: "#e30c26",
        showCancel: false,
        content: '404'
      })
    } else if (res.statusCode == 500) {
      wx.showModal({
        title: '提示',
        confirmColor: "#e30c26",
        showCancel: false,
        content: '服务异常'
      })
    }
  },
  checkedFn: function () {
    var that = this
    that.setData({
      loginType: that.data.loginType ? false : true
    });
  }
})